const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        form: {
            postId: null,
            postCode: '',
            postName: ''
        },
        postId: null,
        postCode: '',
        postName: '',

        step: 1,
        isShowChildren: true,

        menuList: [],
        checkedKeys: [],
        manageMenuIds: [],
    },

    onLoad: function (options) {
        let self = this;
        if (options.postId) {
            wx.setNavigationBarTitle({
                title: '修改岗位'
            });
            app.fetchApis(self, qCloud.api.getPostInfo + options.postId, {}, "GET", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        form: e.data.data,
                    })

                    let params = {
                        resourceId: 10004,
                        tagType: '0',
                        roleId: e.data.data.manageRoleId
                    }
                    self.getRoleMenuTreeSelect(params);
                }
            })
        } else {
            let params = {
                resourceId: 10004,
                tagType: '0'
            }
            this.getMenuTree(params);
        }

    },

    /** 岗位名称输入*/
    postNameInput: function (e) {
        this.data.form.postName = e.detail.value;
    },

    handleClick(e) {
        this.setData({
            manageMenuIds: e.detail.idList
        })
    },

    /**
     * 岗位编码输入
     * @param e
     */
    postCodeInput: function (e) {
        this.data.form.postCode = e.detail.value;
    },

    /**
     * 获取用户菜单
     * @param userId
     */
    getMenuTree: function (params) {
        let self = this;
        app.fetchApis(self, qCloud.api.getMenuTree, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    menuList: e.data.data[0].children
                })
            }
        })
    },

    getRoleMenuTreeSelect: function (params){
        let self = this;
        app.fetchApis(self, qCloud.api.getRoleMenuTreeSelect, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    menuList: e.data.menus[0].children,
                    checkedKeys: e.data.checkedKeys,
                    manageMenuIds: e.data.checkedKeys
                })
            }
        })
    },

    /**
     * 保存岗位信息
     */
    addPost: function () {
        let self = this;
        if (self.data.form.postName == "") {
            wx.showToast({
                title: '岗位名称不能为空',
                icon: "error"
            })
            return;
        }

        let params = {
            postCode: "ERP_" + Number.parseInt(Math.random() * 10000000 + ""),
            postName: self.data.form.postName,
            postSort: 0,
            status: "0",
            dataScope: "4",
            manageRoleType: "0",
            manageMenuIds: self.data.manageMenuIds,
            clientRoleType: "1",
            clientMenuIds: [],
        }

        app.fetchApis(self, qCloud.api.addPost, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: "error"
                })
            }
        })
    },

    /**
     * 修改岗位信息
     */
    updatePost: function () {
        let self = this;
        if (self.data.form.postName == "") {
            wx.showToast({
                title: '岗位名称不能为空',
                icon: "error"
            })
            return;
        }

        let params = {
            postId: self.data.form.postId,
            postName: self.data.form.postName,
            postSort: 0,
            status: "0",
            dataScope: "4",
            manageRoleType: "0",
            manageRoleId: self.data.form.manageRoleId,
            manageMenuIds: self.data.manageMenuIds,
            clientRoleType: "1",
            clientMenuIds: [],
        }

        app.fetchApis(self, qCloud.api.editPost, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: "error"
                })
            }
        })
    },

})
